/*
 * randomtestadventure.c
 *
 *  Created on: Feb 8, 2014
 *      Author: Laurye_OSU
 */


#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>

#define NOISY_TEST 1

//most of the structure and random functions were borrowed from betterTestDrawCard


int checkAdventureCard(int p, int handPos, struct gameState *post) {
  int r;
  int errornum=0;
  int card = 7;
  int bonus = 0;
  int drawntreasure = 0;
  int cardDrawn;
  int temphand[MAX_HAND];
  int starthand[MAX_HAND];
  int z;

  //what is my card count for the real function starting?
 // starthand[p] = post->handCount[p];
 // printf("starting hand count for real function %d\n", starthand[p]);


  //make copy before calling the real function
  struct gameState pre;
  memcpy(&pre, post, sizeof(struct gameState)); //make a copy of the real game state into pre


  //what is my card count starting?
 // temphand[p] = pre.handCount[p];
 // printf("starting hand count for test hand %d\n", temphand[p]);

  //call the real function
  //int cardEffect(int card, int choice1, int choice2, int choice3,
  //struct gameState *state, int handPos, int *bonus)
  r = cardEffect(card, 0, 0, 0, post, handPos, bonus);

//Code is from dominion.c
      while(drawntreasure < 2){
    	  if (pre.deckCount[p] < 1) {
    		  shuffle(p, &pre);
    	  }
    	  drawCard(p, &pre);
    	  cardDrawn = pre.hand[p][pre.handCount[p]-1];//top card of hand is most recently drawn card.
    	  if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold){
    	  drawntreasure++;
    	  }

    	  else{
    		  temphand[z]=cardDrawn;
    		  	 pre.handCount[p]--; //this should just remove the top card (the most recently drawn one).
    		  	 z++;
    	  	  }
      	  }
      while(z-1 >= 0){
    	  pre.discard[p][pre.discardCount[p]++]=temphand[z-1]; // discard all cards in play that have been drawn
    	  z=z-1;
      }

      	  discardCard(handPos, p, &pre, 0);

      	  //what are my hand counts after adventurer. I should have 1 more card because I have to discard
    	 // printf("real function hand count %d\n", post->handCount[p]);
    	 // printf("test function hand count %d\n", pre.handCount[p]);

      //compare test with the actual function
      if (memcmp(&pre, post, sizeof(struct gameState)) != 0){
      	  errornum++;
      	printf("Test failed because real function did not match the test results: %d\n", errornum);
        }

  return 0;

}

int main() {
	int i;
	int j;
	int p;
	int n;
	int handPos;

	struct gameState G;

	printf ("Testing AdventureCard:\n");

	//exact copy from betterTestDrawCard.c
	SelectStream(2);
	PutSeed(3);

	for (n = 0; n < 2000; n++) {
	    for (i = 0; i < sizeof(struct gameState); i++) {
	      ((char*)&G)[i] = floor(Random() * 256);
	    }

	// end of exact copy


	 p = 2; //make number of players 2
	 G.whoseTurn = p; //make whose turn be player p

	 //set the deck count for a player to a random number
	 G.deckCount[p] = floor(Random() * MAX_DECK);
	 	 //go through the player deck
	 	 for (j=0; j < G.deckCount[p]; j++){
	 		 G.deck[p][j] = floor(Random() * 25);
	 	 }

	 //set the discard deck count for a player to a random number
	 G.discardCount[p] = floor(Random() * MAX_DECK);
	 //go through the player discard deck
	 	 	 for (j=0; j < G.discardCount[p]; j++){
	 	 		 G.discard[p][j] = floor(Random() * 25);
	 	 	 }

	 //set the hand count for a player
	 G.handCount[p] = 5;


	 //set the played card count for a player to a random number
	 G.playedCardCount = floor(Random() * MAX_DECK);
	 //go through the player hand
	 	 	 	 for (j=0; j < G.playedCardCount; j++){
	 	 	 	 		 G.playedCards[j] = floor(Random() * 25);
	  	 	 	 }

	 handPos = floor(Random() * G.handCount[p]);  //make hand position random
	 G.hand[p][handPos] = 7; //make the card be adventurer

	 checkAdventureCard(p, handPos, &G);
	}


	 printf ("ALL TESTS OK\n");

	return 0;
}
